package com.ruitu.health_2.ui.common;

import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.arad.base.base_list.BaseRefreshActivity;
import com.ruitu.arad.support.widget.progress.ProgressLayout;
import com.ruitu.health_2.R;
import com.ruitu.health_2.mvp.model.ArticleModel;
import com.ruitu.health_2.mvp.presenter.ArticlePresenter;
import com.ruitu.health_2.ui.adapter.ModelArticleListAdapter;
import com.ruitu.router_module.Router;
import com.ruitu.router_module.bean.PageModel2;
import com.ruitu.router_module.bean.article.Article;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

// 文章列表
public class ArticleListActivity<P,M> extends BaseRefreshActivity<ArticlePresenter.ArticleListP, ArticleModel.ArticleListM> {
    protected RelativeLayout rl_empty;

    protected ModelArticleListAdapter adapter;
    protected List<Article> articleList = new ArrayList<>();

    protected int currPage = 1;
    protected int totalPage = 1;
    protected String name = "";// 传过来的搜索关键字

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_article_list;
    }

    @Override
    protected void onItemClick(View v, int position) {
        Bundle bundle = new Bundle();
        bundle.putString("id", articleList.get(position).getId());
        try {
            Router.startActivity(ArticleListActivity.this, "com.ruitu.health_1.ui.ArticleDetailsActivity", bundle);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rl_empty = findViewById(R.id.rl_empty);

        ((TextView) (rl_empty.findViewById(R.id.txt_description))).setText("暂时没有找到相关文章");

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            name = bundle.getString("name");
        }

        setHeadTitle("文章列表");

        reqDataList();
        showProgress();
    }

    protected void showOrHideEmptyView() {
        if (isDataEmpty(articleList)) {
            rl_empty.setVisibility(View.VISIBLE);
        } else {
            rl_empty.setVisibility(View.GONE);
        }
    }

    protected void reqDataList() {
//        p.reqVideoList(20, currPage);
        p.reqArticleListByTitle(20, currPage, name);
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {
            if (currPage == 1) {
                articleList.clear();
            }
            PageModel2<Article> articlePage = (PageModel2<Article>) data;
            totalPage = articlePage.getTotalPage();
            List<Article> newData = articlePage.getList();
            articleList.addAll(newData);
            adapter.setDataList(articleList);

            showOrHideEmptyView();
        }
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new ModelArticleListAdapter(this);
        return adapter;
    }

    @Override
    protected LRecyclerView findRecyclerView() {
        return findViewById(R.id.lrcv_list);
    }

    @Override
    protected void reqFirst() {
        currPage = 1;
        reqDataList();
    }

    @Override
    protected void reqMore() {
        currPage++;
        if (currPage <= totalPage) {
            reqDataList();
        } else {
            refreshNoMoreData();
        }
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }
}
